package fr.pud.client.view.mapstation;

import javax.swing.ImageIcon;

import fr.pud.client.images.ImagesDatabase;
import fr.pud.utils.Tools;

/**
 * This class implements the singleton pattern to be sure that only one
 * background is going to be mapped in the memory. It enables to save some
 * memory for the other applications
 * @author Pierre Reliquet
 */
public class BubbleSubBackground {
    public static final int            RIGHT_SUB_BUBBLE      = 1;
    public static final int            LEFT_SUB_BUBBLE       = 2;
    public static final int            CENTER_TOP_SUB_BUBBLE = 3;
    public static final int            TOP_RIGHT_SUB_BUBBLE  = 5;
    public static final int            TOP_LEFT_SUB_BUBBLE   = 4;
    public static final int            CENTER_SUB_BUBBLE     = 6;
    /**
     * An image of a bubble where the adress will be written
     */
    private static BubbleSubBackground singletonCenter       = null;
    private static BubbleSubBackground singletonRight        = null;
    private static BubbleSubBackground singletonLeft         = null;
    private static BubbleSubBackground singletonTopCenter    = null;
    private static BubbleSubBackground singletonTopRight     = null;
    private static BubbleSubBackground singletonTopLeft      = null;
    /**
     * The background
     */
    private static ImageIcon           imageSubBubbleCenter;
    /**
     * The background
     */
    private static ImageIcon           imageSubBubbleRight;
    /**
     * The background
     */
    private static ImageIcon           imageSubBubbleLeft;
    /**
     * The background
     */
    private static ImageIcon           imageSubBubbleTopRight;
    /**
     * The background
     */
    private static ImageIcon           imageSubBubbleTopLeft;
    /**
	 * 
	 */
    private static ImageIcon           imageSubBubbleTopCenter;

    /**
     * @return the height of the background
     */
    public static final int getHeight(int subBubbleType) {
        initialization(subBubbleType);
        return imageSubBubbleCenter.getIconHeight();
    }

    /**
     * @return the unique imageIcon instantiated
     */
    public static final ImageIcon getImageIcon(int subBubbleType) {
        initialization(subBubbleType);
        switch (subBubbleType) {
            case LEFT_SUB_BUBBLE:
                return imageSubBubbleLeft;
            case RIGHT_SUB_BUBBLE:
                return imageSubBubbleRight;
            case TOP_LEFT_SUB_BUBBLE:
                return imageSubBubbleTopLeft;
            case TOP_RIGHT_SUB_BUBBLE:
                return imageSubBubbleTopRight;
            case CENTER_TOP_SUB_BUBBLE:
                return imageSubBubbleTopCenter;
            default:
                return imageSubBubbleCenter;
        }
    }

    /**
     * @return the width of the background
     */
    public static final int getWidth(int subBubbleType) {
        initialization(subBubbleType);
        return imageSubBubbleCenter.getIconWidth();
    }

    /**
     * The method which tests if the singleton has already been implemented.
     */
    private static void initialization(int subBubbleType) {
        switch (subBubbleType) {
            case LEFT_SUB_BUBBLE:
                if (singletonLeft == null) {
                    singletonLeft = new BubbleSubBackground(subBubbleType);
                }
                break;
            case RIGHT_SUB_BUBBLE:
                if (singletonRight == null) {
                    singletonRight = new BubbleSubBackground(subBubbleType);
                }
                break;
            case TOP_LEFT_SUB_BUBBLE:
                if (singletonTopLeft == null) {
                    singletonTopLeft = new BubbleSubBackground(subBubbleType);
                }
                break;
            case TOP_RIGHT_SUB_BUBBLE:
                if (singletonTopRight == null) {
                    singletonTopRight = new BubbleSubBackground(subBubbleType);
                }
                break;
            case CENTER_TOP_SUB_BUBBLE:
                if (singletonTopCenter == null) {
                    singletonTopCenter = new BubbleSubBackground(subBubbleType);
                }
                break;
            default:
                if (singletonCenter == null) {
                    singletonCenter = new BubbleSubBackground(subBubbleType);
                }
                break;
        }
    }

    /**
     * The private constructor to be sure that no other instance can be
     * instantiated
     */
    private BubbleSubBackground(int subBubbleType) {
        try {
            switch (subBubbleType) {
                case LEFT_SUB_BUBBLE:
                    imageSubBubbleLeft = ImagesDatabase.IMAGES_SUBBUBBLE_LEFT;
                    break;
                case RIGHT_SUB_BUBBLE:
                    imageSubBubbleRight = ImagesDatabase.IMAGES_SUBBUBBLE_RIGHT;
                    break;
                case TOP_LEFT_SUB_BUBBLE:
                    imageSubBubbleTopLeft = ImagesDatabase.IMAGES_SUBBUBBLE_TOP_LEFT;
                    break;
                case TOP_RIGHT_SUB_BUBBLE:
                    imageSubBubbleTopRight = ImagesDatabase.IMAGES_SUBBUBBLE_TOP_RIGHT;
                    break;
                case CENTER_TOP_SUB_BUBBLE:
                    imageSubBubbleTopCenter = ImagesDatabase.IMAGES_SUBBUBBLE_TOP_CENTER;
                default:
                    imageSubBubbleCenter = ImagesDatabase.IMAGES_SUBBUBBLE;
                    break;
            }
        }
        catch (Exception e) {
            Tools.writeToLogFile("Error while reading bubble.png",
                    Tools.LOG_FILE_NO_JOPTION_PANE, e.getStackTrace());
        }
    }
}
